/*
 * This file is part of andExplorer, android.ovhoo.com
 * Copyright (C) 2007 Mohamed ZALIM <mzalim@ovhoo.com>
 *
 * andExplorer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * andExplorer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */
package com.ovhoo.android.fiveexplore;

import com.ovhoo.android.fiveexplore.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Message;
import android.text.InputFilter;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.*;

public class NewDirDialog extends Dialog implements OnClickListener, OnKeyListener{
	
	
	protected EditText dirNameEdit = null;
	protected Button canellButton, okButton;
	protected Message messageOk;


	public NewDirDialog(Context context, Message msgOk) {
		super(context);
		
		this.messageOk = msgOk;
		
		LayoutInflater _inflater=this.getLayoutInflater();
		View _elementView = _inflater.inflate(R.layout.newdirdialogview, null);
		
		this.dirNameEdit = (EditText) _elementView.findViewById(R.id.NEW_DIR_DIALOG_DIR_NAME);
		this.canellButton = (Button) _elementView.findViewById(R.id.NEW_DIR_DIALOG_BUTTON_CANCEL);
		this.okButton = (Button) _elementView.findViewById(R.id.NEW_DIR_DIALOG_BUTTON_OK);
		
		this.setContentView(_elementView);
		
		this.updateOkButton();
		
		//Listener
		this.canellButton.setOnClickListener(this);
		this.okButton.setOnClickListener(this);
		this.dirNameEdit.setOnKeyListener(this);
		
		//InputFilter
		this.dirNameEdit.setFilters(new InputFilter[]{new AlphaNumericInputFilter() });
		; 
	}

	@Override
	public void onClick(View view) {
		if (view == this.okButton){
			this.messageOk.obj = (String) this.dirNameEdit.getText().toString();
			this.messageOk.sendToTarget();
			this.dismiss();
		}
		else {
			this.dismiss();
		}
		
	}

	@Override
	public boolean onKey(View view, int keyCode, KeyEvent event) {
		if (view == this.dirNameEdit){
			this.updateOkButton();
			
		}
		return false;
	}

	protected void updateOkButton(){
		if (this.dirNameEdit.getText().length() > 0){
			this.okButton.setEnabled(true);
		}
		else{
			this.okButton.setEnabled(false);
		}
	}

}
